package com.acme.bankapp.domain;
import java.io.Serializable;

import com.acme.bankapp.exceptions.*;
import com.acme.bankapp.service.*;


public class Bank implements Serializable{
	private Client[] clients;
	private int numberOfClients;
	transient private ClientRegistrationListener[] listeners = new ClientRegistrationListener[10];
	private int numberOfListeners;
	
	public int getNumberOfClients() {
		return numberOfClients;
	}
	

	public Client[] getClients() {
		return clients;
	}

	public Bank(ClientRegistrationListener[] listeners, int maxAmoutOfClients) {
		clients = new Client[maxAmoutOfClients];
		this.listeners = listeners.clone();
		numberOfListeners = listeners.length - 1;
		this.listeners[numberOfListeners++] = new ClientRegistrationListener() {
			public void onClientAdded(Client client) {
				client.getClientSituation();
				System.out.println(new java.util.Date());
			}
		};
	}
	
	public void addClient(Client client) throws ClientExistsException {
		for (int i = 0; i < numberOfClients; i++) {
			if (clients[i].equals(client))
				throw new ClientExistsException("Client "+client.getClientSituation()+" exists");
		}
		clients[numberOfClients++] = client;
		for (int i = 0; i < numberOfListeners; i++) {
			listeners[i].onClientAdded(client);
		}
	}
	
}
